/***********************************************
 This file is part of redia-lib-utils-ios
 
 
 Copyright (c) 2015 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

#import <Foundation/Foundation.h>
#import "JSONRPCClientDelegate.h"

typedef void (^CompletionHandlerBlock)(NSInteger reqId, BOOL succeeded, id data);

/**
 This class represents a client connections to an JSON RPC server.
 
 @note This class holds a strong reference to its delegate. This is done so that it
 is possible to create a RPC client and hook up an anonymous delegate (like 
 NSUrlConnection does).
 */
@interface JSONRPCClient : NSObject

/**
 The URL of the RPC server.
 */
@property (strong, nonatomic) NSURL *serverURL;

/**
 The delegate adhering to the JSONRPCClientDelegate protocol. This is a strong reference.
 */
@property (strong, nonatomic) id<JSONRPCClientDelegate> delegate;

/**
 The block to call upon completion. This is used _instead_ of a delegate, you can't do both.
 */
@property (strong, nonatomic) CompletionHandlerBlock completionHandlerBlock;

+ (JSONRPCClient*)JSONRPCClientWithServer:(NSURL*)serverURL delegate:(id<JSONRPCClientDelegate>)delegate;
+ (JSONRPCClient*)JSONRPCClientWithServer:(NSURL*)serverURL block:(CompletionHandlerBlock)block;

+ (void)performMethod:(NSString*)method withParams:(NSArray*)params onServer:(NSURL*)server onCompletion:(CompletionHandlerBlock)block;
- (void)performMethod:(NSString *)method withParams:(NSArray *)params onCompletion:(CompletionHandlerBlock)block;

+ (void)stopAllRequests;
- (void)stopRequests;

+ (void)removeConnection:(NSURLConnection*)connection;
- (void)removeConnection:(NSURLConnection*)connection;

- (id)init;

- (BOOL)performMethod:(NSString *)method args:(id)args requestId:(NSInteger)requestId;

- (BOOL)addMethodToQueue:(NSString *)method args:(id)args requestId:(NSInteger)requestId;
- (NSInteger)dispatch;

- (void)addCustomHttpHeader:(NSString*)key value:(NSString*)value;
- (BOOL)removeCustomHttpHeader:(NSString*)key;
- (void)removeAllCustomHttpHeaders;

-(NSArray*)getRequests;

@end
